use std::path::{Path, PathBuf};

pub mod lexer;
pub mod reader;
pub mod tsast;

fn main() {
	let root: PathBuf = std::env::var("OHOS_NDK_HOME").unwrap().into();
	let ets = root.join("ets");

	let mut count = 0;
	let api = ets.join("api");
	parse_dir(&api, &mut count).unwrap();

	let kits = ets.join("kits");
	parse_dir(&kits, &mut count).unwrap();

	let component = ets.join("component");
	parse_dir(&component, &mut count).unwrap();

	println!("file count {}", count);
}

fn parse_dir(dir: &Path, count: &mut usize) -> std::io::Result<()> {
	for entry in std::fs::read_dir(dir)? {
		let entry = entry?;
		let path = entry.path();
		if path.is_file() && path.extension() == Some("ts".as_ref()) {
			*count = *count + 1;
			println!("文件: {}", path.display());
			parse_file(&path)?;
		} else if path.is_dir() {
			parse_dir(&path, count)?;
		}
	}
	Ok(())
}

fn parse_file(file: &Path) -> std::io::Result<()> {
	use tsast::Parse;
	let source = std::fs::read_to_string(file).unwrap();
	let mut lexer = lexer::Lexer::new(&source);
	match tsast::DeclFile::parse(&mut lexer) {
		Ok(_) => {}
		Err(e) => println!("{:#?}", e),
	}
	Ok(())
}
